<template>
  <h1>注册页面</h1>
  <el-card style="width: 500px;margin: 0 auto;">
    <el-form label-width="100px">
      <el-form-item label="用户名"><el-input placeholder="请输入用户名" v-model="user.username"></el-input> </el-form-item>
      <el-form-item label="密码"><el-input type="password" placeholder="请输入密码" v-model="user.password"></el-input> </el-form-item>
      <el-form-item label="昵称"><el-input placeholder="请输入昵称" v-model="user.nickname"></el-input> </el-form-item>
      <el-form-item ><el-button @click="reg()">注册</el-button> </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup>
import {ref} from "vue";
import axios from "axios";
import qs from 'qs';
import {ElMessage} from "element-plus";
import router from "@/router";
const user=ref({username:"",password:"",nickname:""});
const reg=()=>{
  console.log(user);
  let data=qs.stringify(user.value);
  axios.post('http://localhost:8081/v1/users/reg',data)
      .then(
          (response)=>{
            if (response.data.code==2001){

              ElMessage.success("注册成功")
              router.push('/login');
            }else {
              ElMessage.error("用户名已存在！")
            }
          }
      )
}
</script>
